# coding=utf8
__author__ = 'lijian'
import rtu
from utils import byteUtil

# rtu.manager.loadData()

# 模拟量指查询
def fn_11(dataUnit):
    raw = dataUnit.rawData
    count = raw[0]
    start = raw[1]

    offset = 2
    for i in range(count):
        raw = dataUnit.rawData[offset:]
        point = raw[0]
        type = raw[1]
        value = rtu.manager.decodeAnalogByType(type, raw[2:4])

        print "point:", point, "type:", type, "value:", value

        offset += 4


# 状态量指查询
def fn_12(dataUnit):
    raw = dataUnit.rawData
    count = raw[0]
    start = raw[1]

    offset = 2
    for i in range(count):
        point = raw[offset]
        offset += 1
        value = raw[offset]
        offset += 1

        print "point:", point, "value:", value


# 单灯运行状态查询
def fn_33(dataUnit):
    raw = dataUnit.rawData

    state = {
        0x00: "关灯",
        0x01: "正常开灯",
        0x02: "节能开灯状态"
    }

    run = {
        0x00: "正常",
        0x01: "灯亮但有异常",
        0x02: "异常灭灯",
        0x03: "其它"
    }

    print "开关状态:", state[raw[0]], "运行状态:", run[raw[1]]


# 单灯电压
def fn_34(dataUnit):
    print "电压:", byteUtil.decodeA7(dataUnit.rawData)


# 单灯电流
def fn_35(dataUnit):
    print "电流:", byteUtil.decodeA25(dataUnit.rawData)


#单灯有功功率
def fn_36(dataUnit):
    print "有功功率:", byteUtil.decodeA2(dataUnit.rawData)


#单灯功率因素
def fn_37(dataUnit):
    print "功率因素:", byteUtil.decodeA5(dataUnit.rawData)

#单灯视在功率
def fn_38(dataUnit):
    print "视在功率:", byteUtil.decodeA2(dataUnit.rawData)


#单灯无功功率
def fn_39(dataUnit):
    print "无功功率:", byteUtil.decodeA2(dataUnit.rawData)